package com.qingxin.sys.verifier.rules;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.qingxin.sys.verifier.FormMap;
import com.qingxin.sys.verifier.VerifyMsg;
import com.qingxin.sys.verifier.VerifyRule;

/**
 * 值域范围验证
 * @author Administrator
 *
 */
public class InputRule extends VerifyRule {

	private String type;//size/number/string/date/datetime/checked/selected
	private String min;
	private String max;
	
	
	public InputRule() {
		this.type = "size";
		this.min =  "";
		this.max = "";
	}
	
	
	@Override
	public VerifyMsg isValid(String id, String name, String label, FormMap formMap) {
		String value = formMap.getParameter(name);
		if(value == null){
			value = "";
		}
		if("size".equalsIgnoreCase(type) || "checked".equalsIgnoreCase(type) || "selected".equalsIgnoreCase(type)){
			int len = value.length();
			if("checked".equalsIgnoreCase(type) || "selected".equalsIgnoreCase(type)){
				len = 0;
				String[] values = formMap.getParameterValues(name);
				if(values != null){
					len = values.length;
				}
			}
			int _min = -1;
			int _max = -1;
			if(!isEmpty(min)){
				try {
					_min = Integer.parseInt(min);
				} catch (Exception e) {}
			}
			if(!isEmpty(max)){
				try {
					_max = Integer.parseInt(max);
				} catch (Exception e) {}
			}
			if(_min == -1 && _max == -1){
				return null;//没有配置时忽略该规则
			}
			if((_min > -1 && len < _min) || (_max > -1 && len > _max)){
				String message = createMessage(null, "{name}", label, "{label}", label, "{min}", _min == -1 ? "" : min, "{max}", _max == -1 ? "" : max);
				return new VerifyMsg(id, name, label, this.getClass().getSimpleName(), message);
			}else{
				return null;
			}
		}else if("number".equalsIgnoreCase(type)){
			BigDecimal _val = null;
			BigDecimal _min = null;
			BigDecimal _max = null;
			try {
				_val = new BigDecimal(value);
			} catch (Exception e) {}
			if(_val == null){
				return null;//没有值时忽略该规则
			}
			if(!isEmpty(min)){
				try {
					_min = new BigDecimal(min);
				} catch (Exception e) {}
			}
			if(!isEmpty(max)){
				try {
					_max = new BigDecimal(max);
				} catch (Exception e) {}
			}
			if(_min == null && _max == null){
				return null;//没有配置时忽略该规则
			}
			if((_min != null && _val.compareTo(_min) < 0) || (_max != null && _val.compareTo(_max) > 0)){
				String message = createMessage(null, "{name}", label, "{label}", label, "{min}", _min == null ? "" : min, "{max}", _max == null ? "" : max);
				return new VerifyMsg(id, name, label, this.getClass().getSimpleName(), message);
			}else{
				return null;
			}
		}else if("string".equalsIgnoreCase(type)){
			if(isEmpty(min) && isEmpty(max)){
				return null;//没有配置时忽略该规则
			}
			if(value.compareTo(min) < 0 || value.compareTo(max) > 0){
				String message = createMessage(null, "{name}", label, "{label}", label, "{min}", isEmpty(min) ? "" : min, "{max}", isEmpty(max) ? "" : max);
				return new VerifyMsg(id, name, label, this.getClass().getSimpleName(), message);
			}else{
				return null;
			}
		}else if("date".equalsIgnoreCase(type) || "datetime".equalsIgnoreCase(type)){
			Date _val = null;
			Date _min = null;
			Date _max = null;
			
			SimpleDateFormat sdf = null;
			if ("date".equalsIgnoreCase(type)) {
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			}else{
				sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
			
			try {
				_val = sdf.parse(value);
			} catch (Exception e) {}
			if(_val == null){
				return null;//没有值时忽略该规则
			}
			if(!isEmpty(min)){
				try {
					_min = sdf.parse(min);
				} catch (Exception e) {}
			}
			if(!isEmpty(max)){
				try {
					_max = sdf.parse(max);
				} catch (Exception e) {}
			}
			if(_min == null && _max == null){
				return null;//没有配置时忽略该规则
			}
			if((_min != null && _val.compareTo(_min) < 0) || (_max != null && _val.compareTo(_max) > 0)){
				String message = createMessage(null, "{name}", label, "{label}", label, "{min}", _min == null ? "" : min, "{max}", _max == null ? "" : max);
				return new VerifyMsg(id, name, label, this.getClass().getSimpleName(), message);
			}else{
				return null;
			}
		}
		return null;
	}
	
	
	@Override
	public Map<String, Object> getRuleCfg(String name, String label) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rtype", "input");
		if("size".equalsIgnoreCase(type) || "checked".equalsIgnoreCase(type) || "selected".equalsIgnoreCase(type)){
			int _min = -1;
			int _max = -1;
			if(!isEmpty(min)){
				try {
					_min = Integer.parseInt(min);
				} catch (Exception e) {}
			}
			if(!isEmpty(max)){
				try {
					_max = Integer.parseInt(max);
				} catch (Exception e) {}
			}
			if(_min == -1 && _max == -1){
				return null;
			}
			map.put("type", type);
			if(_min > -1){
				map.put("min", _min);
			}
			if(_max > -1){
				map.put("max", _max);
			}
			
			String errormsg = createMessage(null, "{name}", label, "{label}", label, "{min}", _min == -1 ? "" : min, "{max}", _max == -1 ? "" : max);
			if(!isEmpty(errormsg)){
				map.put("error", errormsg);
			}
		}else if("number".equalsIgnoreCase(type)){
			BigDecimal _min = null;
			BigDecimal _max = null;
			if(!isEmpty(min)){
				try {
					_min = new BigDecimal(min);
				} catch (Exception e) {}
			}
			if(!isEmpty(max)){
				try {
					_max = new BigDecimal(max);
				} catch (Exception e) {}
			}
			if(_min == null && _max == null){
				return null;
			}
			map.put("type", type);
			if(_min != null){
				map.put("min", _min);
			}
			if(_max != null){
				map.put("max", _max);
			}
			
			String errormsg = createMessage(null, "{name}", label, "{label}", label, "{min}", _min == null ? "" : min, "{max}", _max == null ? "" : max);
			if(!isEmpty(errormsg)){
				map.put("error", errormsg);
			}
		}else if("string".equalsIgnoreCase(type)){
			if(isEmpty(min) && isEmpty(max)){
				return null;
			}
			map.put("type", type);
			if(!isEmpty(min)){
				map.put("min", min);
			}
			if(!isEmpty(max)){
				map.put("max", max);
			}
			
			String errormsg = createMessage(null, "{name}", label, "{label}", label, "{min}", isEmpty(min) ? "" : min, "{max}", isEmpty(max) ? "" : max);
			if(!isEmpty(errormsg)){
				map.put("error", errormsg);
			}
		}else if("date".equalsIgnoreCase(type) || "datetime".equalsIgnoreCase(type)){
			Date _min = null;
			Date _max = null;
			
			SimpleDateFormat sdf = null;
			if ("date".equalsIgnoreCase(type)) {
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			}else{
				sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
			if(!isEmpty(min)){
				try {
					_min = sdf.parse(min);
				} catch (Exception e) {}
			}
			if(!isEmpty(max)){
				try {
					_max = sdf.parse(max);
				} catch (Exception e) {}
			}
			if(_min == null && _max == null){
				return null;
			}
			map.put("type", type);
			if(_min != null){
				map.put("min", _min.getTime());
			}
			if(_max != null){
				map.put("max", _max.getTime());
			}
			
			String errormsg = createMessage(null, "{name}", label, "{label}", label, "{min}", _min == null ? "" : min, "{max}", _max == null ? "" : max);
			if(!isEmpty(errormsg)){
				map.put("error", errormsg);
			}
		}
		return map;
	}
	

	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getMin() {
		return min;
	}
	public void setMin(String min) {
		this.min = min;
	}
	public String getMax() {
		return max;
	}
	public void setMax(String max) {
		this.max = max;
	}
	
	
}
